package l2hc.gameserver.data.xml.parser;

import java.io.File;
import java.util.Iterator;

import l2hc.commons.data.xml.AbstractFileParser;
import l2hc.gameserver.Config;
import l2hc.gameserver.data.xml.holder.SuperPointHolder;
import l2hc.gameserver.model.superPoint.SuperPoinCoordinate;
import l2hc.gameserver.model.superPoint.SuperPoint;
import l2hc.gameserver.model.superPoint.SuperPointType;
import l2hc.gameserver.network.serverpackets.components.ChatType;

import org.dom4j.Element;

public final class SuperPointParser extends AbstractFileParser<SuperPointHolder>
{
  private static SuperPointParser _instance = new SuperPointParser();

  public static SuperPointParser getInstance()
  {
    return _instance;
  }

  protected SuperPointParser()
  {
    super(SuperPointHolder.getInstance());
  }

  public File getXMLFile()
  {
    return new File(Config.DATAPACK_ROOT, "data/superpointinfo.xml");
  }

  public String getDTDFileName()
  {
    return "superpointinfo.dtd";
  }

  public static String stringValue(String t)
  {
    return t;
  }

  public static String stringValue(String t, String def)
  {
    return t == null ? def : stringValue(t);
  }

  @SuppressWarnings("rawtypes")
protected void readData(Element rootElement)
    throws Exception
  {
    for (Iterator iterator = rootElement.elementIterator(); iterator.hasNext(); )
    {
      Element element = (Element)iterator.next();
      if ("superpoint".equals(element.getName()))
      {
        SuperPoint point = new SuperPoint();
        String pointName = element.attributeValue("name");
        point.setType(SuperPointType.valueOf(stringValue(element.attributeValue("moveType"), "NONE")));
        for (Iterator iter = element.elementIterator(); iter.hasNext(); )
        {
          Element element2 = (Element)iter.next();

          if ("point".equalsIgnoreCase(element2.getName()))
          {
            int x = Integer.parseInt(element2.attributeValue("x"));
            int y = Integer.parseInt(element2.attributeValue("y"));
            int z = Integer.parseInt(element2.attributeValue("z"));

            SuperPoinCoordinate coords = new SuperPoinCoordinate(x, y, z);

            for (Iterator iter2 = element2.elementIterator(); iter2.hasNext(); )
            {
              Element element3 = (Element)iter2.next();
              if ("msg".equalsIgnoreCase(element3.getName()))
              {
                int id = Integer.parseInt(element3.attributeValue("id"));
                int radius = Integer.parseInt(element3.attributeValue("radius"));
                if (radius == 0) {
                  radius = 1500;
                }
                coords.setMsgId(id);
                coords.setMsgChatType(ChatType.valueOf(stringValue(element3.attributeValue("chat"), "ALL")));
               coords.setMsgRadius(radius);
              }
              else if ("delay".equalsIgnoreCase(element3.getName()))
              {
                int sec = Integer.parseInt(element3.attributeValue("sec"));
                coords.setDelayInSec(sec);
              }
              else if ("social".equalsIgnoreCase(element3.getName()))
              {
                int id = Integer.parseInt(element3.attributeValue("id"));
                coords.setSocialId(id);
              }
            }
            point.addMoveCoordinats(coords);
          }
        }

        getHolder().addSuperPoints(pointName, point);
      }
    }
  }
}
